package com.qcloud.weapp.demo.servlet;

import com.qcloud.cos.exception.AbstractCosException;
import com.qcloud.cos.sign.Credentials;
import com.qcloud.cos.sign.Sign;
import com.qcloud.weapp.authorization.UserInfo;
import com.qcloud.weapp.demo.utils.CosAuthFactory;
import com.qcloud.weapp.demo.utils.JSONSerializer;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/cosAuth")
public class CosAuthServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private final String dir = "/uploadCos";

    /**
     * 处理登录请求
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过 ServletRequest 和 ServletResponse 初始化登录服务
        String type = request.getParameter("type");
        //LoginService service = new LoginService(request, response);
        Map<String,Object> result = null;
        try {
            // 调用登录接口，如果登录成功可以获得登录信息
            //UserInfo userInfo = service.check();
            if (!StringUtils.isBlank(type)) {
                if (type.equals("download")){
                    downLoad(request,response);
                }else if (type.equals("period")){
                    period(request,response);
                }else if (type.equals("one")){
                    one(request,response);
                }else {
                    result = new HashMap<>();
                }
            }
        } catch (AbstractCosException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取下载鉴权(多次有效)
     * @param request
     * @param response
     * @param userInfo
     */
    private void downLoad(HttpServletRequest request, HttpServletResponse response) throws AbstractCosException, IOException {
        Credentials cred = CosAuthFactory.getCredentials();
        long expired = System.currentTimeMillis() / 1000 + 600;
        String signStr = Sign.getDownLoadSign(CosAuthFactory.getBacket(), dir, cred, expired);
        Map<String,Object> result = new HashMap<>();
        result.put("dir",dir);
        result.put("sign",signStr);
        result.put("expired",expired);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(JSONSerializer.serialize(result));
    }

    /**
     * 获取多次鉴权
     * @param request
     * @param response
     * @param userInfo
     */
    private void period(HttpServletRequest request, HttpServletResponse response) throws AbstractCosException, IOException {
        Credentials cred = CosAuthFactory.getCredentials();
        long expired = System.currentTimeMillis() / 1000 + 600;
        String signStr = Sign.getPeriodEffectiveSign(CosAuthFactory.getBacket(), dir , cred,expired);
        Map<String,Object> result = new HashMap<>();
        result.put("dir",dir);
        result.put("sign",signStr);
        result.put("expired",expired);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(JSONSerializer.serialize(result));
    }

    /**
     * 获取单次鉴权
     * @param request
     * @param response
     * @param userInfo
     * @throws AbstractCosException
     * @throws java.io.IOException
     */
    private void one(HttpServletRequest request, HttpServletResponse response) throws AbstractCosException, IOException {
        Credentials cred = CosAuthFactory.getCredentials();
        String signStr = Sign.getOneEffectiveSign(CosAuthFactory.getBacket(), dir, cred);
        Map<String,Object> result = new HashMap<>();
        result.put("dir",dir);
        result.put("sign",signStr);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(JSONSerializer.serialize(result));
    }


}
